package com.example.datacollection.controller;

import com.example.datacollection.bean.SysUser;
import com.example.datacollection.config.ResultMap;
import com.example.datacollection.constants.ConstantsInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("login")
@Slf4j
public class LoginController {
    @RequestMapping("/toLogin")
    public Map toLogin(SysUser user) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户登陆数据
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getPassWord());
        try {
            subject.login(token);//执行登陆方法，如果无异常说明登录成功
        } catch (UnknownAccountException e) {//用户名不存在
            log.info("捕获用户名不存在异常");
            return ResultMap.error("message", "用户名不存在");
        } catch (IncorrectCredentialsException e) {//密码错误
            return ResultMap.error("message", "用户密码错误");
        }
        return ResultMap.ok();
    }
}
